package cn.lg.soar.system.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.database.util.MpSqlUtils;
import cn.lg.soar.database.util.PageUtils;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.mvc.enums.ControllerConst;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.auth.entity.Menu;
import cn.lg.soar.system.auth.model.MenuTree;
import cn.lg.soar.system.auth.model.MenuVo;
import cn.lg.soar.system.auth.service.IMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Api(tags = "菜单管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/menu")
public class MenuController extends BaseController<Menu, IMenuService> {

    @Override
    protected Serializable getId(Menu entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "菜单";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated MenuVo menu) {
        service.create(menu);
        return HttpResult.success();
    }

    @Log(value = "修改" + SUBJECT, data = "#p0", dataId = "#p0.id")
    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated MenuVo menu) {
        service.update(menu);
        return HttpResult.success();
    }

    @Log(value = "删除" + SUBJECT, data = "#p0", type = Log.Type.TEXT)
    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DetailMapping
    public HttpResult<MenuVo> detail(@Id Long id) {
        MenuVo vo = service.detail(id);
        return HttpResult.data(vo);
    }

    @QueryMapping
    public HttpResult<PageResult<Menu>> query(PageQuery pageQuery, QueryParams params, Long[] roleIds){
        QueryWrapper<Menu> wrapper = new SqueryWapper<>(Menu.class).listWrapper(params);
        MpSqlUtils.handleInOrEq(roleIds, (sql, value) -> {
            wrapper.exists("select * from lg_sys_role_2_menu m where lg_sys_menu.id=m.menu_id and m.role_id " + sql, value);
        });
        IPage<Menu> page = service.page(PageUtils.toPage(pageQuery), wrapper);
        return HttpResult.data(PageUtils.toPageResult(page));
    }

    @ListMapping
    public HttpResult<List<Menu>> list(QueryParams params){
        return HttpResult.data(super.getList(params));
    }

    @ApiOperation(value = ControllerConst.LIST_NAME, notes = "树结构")
    @GetMapping("tree")
    public HttpResult<List<MenuTree>> tree(QueryParams params){
        List<MenuTree> tree = service.tree();
        return HttpResult.data(tree);
    }

    @ExistMapping
    public HttpResult<Boolean> exist(QueryParams params){
        return super.existResult(params);
    }

    @Log(value = "导出" + SUBJECT, data = "#p0")
    @ExportMapping
    public void exports(QueryParams params, HttpServletResponse resp){
        super.exportExcel(MenuController.SUBJECT, params, resp);
    }

    @Log(value = "导入" + SUBJECT)
    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ApiOperation("清空缓存")
    @PostMapping(value = "clear-cache", name = "清空缓存")
    public HttpResult<Void> clearCache(){
        this.service.clearCache();
        return HttpResult.success();
    }

}
